﻿using Abp.Domain.Services;
using shenghua.Basics.MemberLevels;
using shenghua.Common.CustomValueType;
using shenghua.Common.FreeValues;
using shenghua.Member.XP.Median;
using shenghua.Order.Orders.Median;
using shenghua.Basics.ShippingAddresses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using shenghua.Basics.ShippingTemplates;

namespace shenghua.Basics
{
    /// <summary>
    /// 后台设置
    /// </summary>
    public class SettingManager: DomainService
    {
        private readonly FreeValueManager _freeValueManager;
        private readonly MemberLevelManager _memberLevelManager;

        public SettingManager(
            FreeValueManager freeValueManager,
            MemberLevelManager memberLevelManger)
        {
            _freeValueManager = freeValueManager;
            _memberLevelManager = memberLevelManger;
        }

        #region 商城设置
        // 购买可见状态
        public ShoppingVisibleType GetShoppingVisibleType()
        {
            return (ShoppingVisibleType)int.Parse(_freeValueManager.GetValue("website.setting.ship.shoppingvisibletype", ((int)ShoppingVisibleType.All).ToString()));
        }
        public Task<FreeValue> SetShoppingVisibleType(ShoppingVisibleType value)
        {
            return _freeValueManager.SetValue("website.setting.ship.shoppingvisibletype", ((int)value).ToString());
        }
        // 库存扣减方式
        public InventoryDeductionType GetInventoryDeductionType()
        {
            return (InventoryDeductionType)int.Parse(_freeValueManager.GetValue("website.setting.ship.inventorydeductiontype", ((int)InventoryDeductionType.Ordered).ToString()));
        }
        public Task<FreeValue> SetInventoryDeductionType(InventoryDeductionType value)
        {
            return _freeValueManager.SetValue("website.setting.ship.inventorydeductiontype", ((int)value).ToString());
        }
        #endregion

        #region 配送设置
        // 物流信息跟踪
        public bool GetEnabledTrackWuliu()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.ship.enabledtrackwuliu", "false"));
        }
        public Task<FreeValue> SetEnabledTrackWuliu(bool value)
        {
            return _freeValueManager.SetValue("website.setting.ship.enabledtrackwuliu", value);
        }
        // 当发货时发送短信
        public bool GetEnabledSendSMSWhenShipped()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.ship.sendsmswhenshipped", "false"));
        }
        public Task<FreeValue> SetEnabledSendSMSWhenShipped(bool value)
        {
            return _freeValueManager.SetValue("website.setting.ship.sendsmswhenshipped", value);
        }
        // 短信模板
        public string GetSMSTemplate()
        {
            return _freeValueManager.GetValue("website.setting.ship.smstemplate");
        }
        public Task<FreeValue> SetSMSTemplate(string value)
        {
            return _freeValueManager.SetValue("website.setting.ship.smstemplate", value);
        }
        // 运费模板类型
        public ShippingTemplateType GetShippingTemplateType()
        {
            return (ShippingTemplateType)int.Parse(_freeValueManager.GetValue("website.setting.ship.shippingtemplatetype", ((int)ShippingTemplateType.FixedFreight).ToString()));
        }
        public Task<FreeValue> SetShippingTemplateType(ShippingTemplateType value)
        {
            return _freeValueManager.SetValue("website.setting.ship.shippingtemplatetype", ((int)value).ToString());
        }
        #endregion

        #region 积分设置
        // 是否消费获得积分
        public bool GetCanGainXPFromConsumption()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.xp.cangainxpfromconsumption", "true"));
        }
        public Task<FreeValue> SetCanGainXPFromConsumption(bool value)
        {
            return _freeValueManager.SetValue("website.setting.xp.cangainxpfromconsumption", value.ToString());
        }
        // 消费一元获得多少积分
        public int GetXPGainedByOneAmount()
        {
            return int.Parse(_freeValueManager.GetValue("website.setting.xp.xpgainedbyoneamount", "0"));
        }
        public Task<FreeValue> SetXPGainedByOneAmount(int value)
        {
            return _freeValueManager.SetValue("website.setting.xp.xpgainedbyoneamount", value.ToString());
        }
        // 是否按订单总价计算积分
        public bool GetIsXPGainedByOrderTotalAmount()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.xp.isxpgainedbyordertotalamount", "true"));
        }
        public Task<FreeValue> SetIsXPGainedByOrderTotalAmount(bool value)
        {
            return _freeValueManager.SetValue("website.setting.xp.isxpgainedbyordertotalamount", value.ToString());
        }
        // 是否产品单独设置积分
        public bool GetIsXPGainedByCommodity()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.xp.isxpgainedbycommodity", "false"));
        }
        public Task<FreeValue> SetIsXPGainedByCommodity(bool value)
        {
            return _freeValueManager.SetValue("website.setting.xp.isxpgainedbycommodity", value.ToString());
        }

        // 是否登录获得积分
        public bool GetCanGainXPFromLogin()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.xp.cangainxpfromlogin", "true"));
        }
        public Task<FreeValue> SetCanGainXPFromLogin(bool value)
        {
            return _freeValueManager.SetValue("website.setting.xp.cangainxpfromlogin", value.ToString());
        }
        // 登录一次获得多少积分
        public int GetXPGainedByOneLogin()
        {
            return int.Parse(_freeValueManager.GetValue("website.setting.xp.xpgainedbyonelogin", "0"));
        }
        public Task<FreeValue> SetXPGainedByOneLogin(int value)
        {
            return _freeValueManager.SetValue("website.setting.xp.xpgainedbyonelogin", value.ToString());
        }
        
        // 是否使用积分抵消金额
        public bool GetCanOffsetAmountByXP()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.xp.canoffsetamountbyxp", "true"));
        }
        public Task<FreeValue> SetCanOffsetAmountByXP(bool value)
        {
            return _freeValueManager.SetValue("website.setting.xp.canoffsetamountbyxp", value.ToString());
        }
        // 抵消一元所需要的积分
        public int GetXPNeededToOffsetOneAmount()
        {
            return int.Parse(_freeValueManager.GetValue("website.setting.xp.xpneededtooffsetoneamount", "100"));
        }
        public Task<FreeValue> SetXPNeededToOffsetOneAmount(int value)
        {
            return _freeValueManager.SetValue("website.setting.xp.xpneededtooffsetoneamount", value.ToString());
        }
        // 最高积分抵消金额百分比
        public double GetMaxPercentOfOffsetAmount()
        {
            return double.Parse(_freeValueManager.GetValue("website.setting.xp.maxpercentofoffsetamount", "0"));
        }
        public Task<FreeValue> SetMaxPercentOfOffsetAmount(double value)
        {
            return _freeValueManager.SetValue("website.setting.xp.maxpercentofoffsetamount", value.ToString());
        }
        // 单笔订单最多使用积分
        public int GetMaxXPOfOneOrder()
        {
            return int.Parse(_freeValueManager.GetValue("website.setting.xp.maxxpofoneorder", "0"));
        }
        public Task<FreeValue> SetMaxXPOfOneOrder(int value)
        {
            return _freeValueManager.SetValue("website.setting.xp.maxxpofoneorder", value.ToString());
        }
        #endregion

        #region 会员等级设置
        // 最大会员等级
        public async Task<MemberLevel> GetMemberLevelByLevel(int level)
        {
            return await _memberLevelManager.GetByLevel(level);
        }
        public int GetMaxMemberLevel()
        {
            return int.Parse(_freeValueManager.GetValue("website.setting.memberlevel.maxmemberlevel", "1"));
        }
        public Task<FreeValue> SetMaxMemberLevel(int value)
        {
            return _freeValueManager.SetValue("website.setting.memberlevel.maxmemberlevel", value.ToString());
        }
        // 升级方式
        public MemberUpgradeMethod GetMemberUpgradeMethod()
        {
            return (MemberUpgradeMethod)int.Parse(_freeValueManager.GetValue("website.setting.memberlevel.memberupgrademethod", ((int)MemberUpgradeMethod.None).ToString()));
        }
        public Task<FreeValue> SetMaxMemberLevel(MemberUpgradeMethod value)
        {
            return _freeValueManager.SetValue("website.setting.memberlevel.memberupgrademethod", ((int)value).ToString());
        }
        // 优惠方式
        public DiscountMethod GetDiscountMethod()
        {
            return (DiscountMethod)int.Parse(_freeValueManager.GetValue("website.setting.memberlevel.discountmethod", ((int)DiscountMethod.Unified).ToString()));
        }
        public Task<FreeValue> SetDiscountMethod(DiscountMethod value)
        {
            return _freeValueManager.SetValue("website.setting.memberlevel.discountmethod", ((int)value).ToString());
        }
        #endregion

        #region 验证设置
        #endregion

        #region 登录设置
        #endregion

        #region 注册设置
        #endregion

        #region 找回密码设置
        #endregion

        #region 公告设置
        #endregion

        #region 商品设置

        #endregion

        #region 浏览设置
        #endregion

        #region 支付设置
        // 微信可支付
        public bool GetCanWeixinPay()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.payment.canweixinpay", "false"));
        }
        public Task<FreeValue> SetCanWeixinPay(bool value)
        {
            return _freeValueManager.SetValue("website.setting.payment.canweixinpay", value);
        }
        // 支付宝可支付
        public bool GetCanAlipay()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.payment.canalipay", "false"));
        }
        public Task<FreeValue> SetCanAlipay(bool value)
        {
            return _freeValueManager.SetValue("website.setting.payment.canalipay", value);
        }
        public AlipayConfigInfo GetAlipayConfigInfo()
        {
            return _freeValueManager.GetValue("website.setting.payment.alipayconfiginfo", new AlipayConfigInfo());
        }
        public Task<FreeValue> SetAlipayConfigInfo(AlipayConfigInfo value)
        {
            return _freeValueManager.SetValue("website.setting.payment.alipayconfiginfo", value);
        }
        // 可银行转账
        public bool GetCanBankTransfer()
        {
            return bool.Parse(_freeValueManager.GetValue("website.setting.payment.canbanktransfer", "false"));
        }
        public Task<FreeValue> SetCanBankTransfer(bool value)
        {
            return _freeValueManager.SetValue("website.setting.payment.canbanktransfer", value);
        }
        // 银行信息
        public List<BankInfo> GetBankInfos()
        {
            return _freeValueManager.GetValue("website.setting.payment.bankinfos", new List<BankInfo>());
        }
        public Task<FreeValue> SetBankInfos(List<BankInfo> value)
        {
            return _freeValueManager.SetValue("website.setting.payment.bankinfos", value);
        }
        #endregion

        #region 订单设置
        #endregion
    }

    /// <summary>
    /// 会员升级方式
    /// </summary>
    public enum MemberUpgradeMethod
    {
        /// <summary>
        /// 无
        /// </summary>
        None = 0,
        /// <summary>
        /// 根据积分自动升级
        /// </summary>
        AutoByXPValue = 1
    }

    /// <summary>
    /// 优惠方式
    /// </summary>
    public enum DiscountMethod
    {
        /// <summary>
        /// 统一设置商城折扣
        /// </summary>
        Unified = 1,
        /// <summary>
        /// 各产品独立设置
        /// </summary>
        Independent = 2
    }

    /// <summary>
    /// 购买可见状态
    /// </summary>
    public enum ShoppingVisibleType
    {
        All = 1,
        Member = 2
    }

    /// <summary>
    /// 库存扣减方式
    /// </summary>
    public enum InventoryDeductionType
    {
        Paid = 1,
        Ordered = 2
    }

    public class AlipayConfigInfo
    {
        public string AppId { get; set; }
        public string AppPrivateKey { get; set; }
        public string AlipayPublicKey { get; set; }

        public string ReturnUrl { get; set; }
        public string NotifyUrl { get; set; }
    }

    public class BankInfo
    {
        public string BankName { get; set; }
        public string AccountName { get; set; }
        public string BankAccount { get; set; }
        public string Remark { get; set; }
    }
}
